package com.laisheng.interview.service.impl;

import com.laisheng.interview.mapper.EvaluationMapper;
import com.laisheng.interview.model.Evaluation;
import com.laisheng.interview.service.EvaluationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 面试评价服务实现类
 */
@Service
public class EvaluationServiceImpl implements EvaluationService {

    @Autowired
    private EvaluationMapper evaluationMapper;

    @Override
    public Evaluation getByAccountingInfoId(Long accountingInfoId) {
        return evaluationMapper.selectByAccountingInfoId(accountingInfoId);
    }

    @Override
    public boolean saveOrUpdateEvaluation(Long accountingInfoId, String content, String rating) {
        try {
            // 先查询是否已存在评价
            Evaluation existingEvaluation = evaluationMapper.selectByAccountingInfoId(accountingInfoId);
            
            if (existingEvaluation != null) {
                // 更新现有评价
                existingEvaluation.setContent(content);
                existingEvaluation.setRating(rating);
                return evaluationMapper.update(existingEvaluation) > 0;
            } else {
                // 创建新评价
                Evaluation newEvaluation = new Evaluation();
                newEvaluation.setAccountingInfoId(accountingInfoId);
                newEvaluation.setContent(content);
                newEvaluation.setRating(rating);
                return evaluationMapper.insert(newEvaluation) > 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean deleteByAccountingInfoId(Long accountingInfoId) {
        try {
            return evaluationMapper.deleteByAccountingInfoId(accountingInfoId) > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
} 